﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity.Validation;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRBASE
{
    public partial class FMI_TS_PACK : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser, DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        //DBSERVER myADO = new DBSERVER();
        DBCache myCache = new DBCache();
        private int iGridView = 0;
        private string sType = "V";

        public FMI_TS_PACK()
        {
            InitializeComponent();
        }

        //查询代码
        protected override void OnSys04()
        {
            string SQL = "select c.fdictname,a.*"
              + "  from i_ts_pack a"
              + "  inner join s_ts_dict c on c.fdicttype = 3001 and c.fdictno = a.fstorageflag"
              + "  where 1 = 1";
            DataRowView drv = (DataRowView)uiCBfstorageflag.SelectedItem;
            if ((drv != null)&&(uiCBfstorageflag.SelectedIndex>0))
            {
                string sfstorageflag = drv.Row["fDictNo"].ToString();
                SQL = SQL + " and c.fdictno='" + sfstorageflag + "'";
            }

            DataRowView drv2 = (DataRowView)cbfclassstno.SelectedItem;
            if ((drv2 != null) && (cbfclassstno.SelectedIndex > 0))
            {
                string sfclassstno = drv2.Row["fDictNo"].ToString();
                SQL = SQL + " and a.fclassstno='" + sfclassstno + "'";
            }
            if (qifpackno.Text != "")
            {
                SQL = SQL + " and ((a.fpackname like '%" + qifpackno.Text + "%') or (a.fpackno like '%" + qifpackno.Text + "%'))";
            }
            SQL = SQL + "  order by c.fdictname,a.fpackno";
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();
            rowindex = -1;
            
        }

        private void FMI_TS_PACK_Load(object sender, EventArgs e)
        {
            _INPUTTABLE = "import_i_ts_pack";
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];

            //按钮控制
            sys07.Visible = false; sys07.Tag = "1";
            sys08.Visible = false; sys08.Tag = "1";
            sys09.Visible = false; sys09.Tag = "1";
            sys11.Visible = false; sys11.Tag = "1";

            //下拉框
            DataTable myTs_Dict = myCache.doGetDict("S_TS_DICT", "fDictType=3001");
            uifstorageflag.DataSource = myTs_Dict;
            uifstorageflag.DisplayMember = "fDictName";
            uifstorageflag.ValueMember = "fDictNo";

            DataRow dr1 = myTs_Dict.NewRow();
            dr1["fDictName"] = "请选择";
            dr1["fDictNo"] = "0";
            myTs_Dict.Rows.InsertAt(dr1, 0);
            uiCBfstorageflag.DataSource = myTs_Dict;
            uiCBfstorageflag.DisplayMember = "fDictName";
            uiCBfstorageflag.ValueMember = "fDictNo";
            uiCBfstorageflag.SelectedIndex = 0;

            DataTable myTs_Dict2 = myCache.doGetDict("S_TS_DICT", "fDictType=3007");
            uifcolor.DataSource = myTs_Dict2;
            uifcolor.DisplayMember = "fDictName";
            uifcolor.ValueMember = "fDictNo";

            DataTable myTs_Dict3 = myCache.doGetDict("S_TS_DICT", "fDictType=3008");
            uifclassstno.DataSource = myTs_Dict3;
            uifclassstno.DisplayMember = "fDictName";
            uifclassstno.ValueMember = "fDictNo";

            DataRow dr2 = myTs_Dict3.NewRow();
            dr2["fDictName"] = "请选择";
            dr2["fDictNo"] = "0";
            myTs_Dict3.Rows.InsertAt(dr2, 0);
            cbfclassstno.DataSource = myTs_Dict3;
            cbfclassstno.DisplayMember = "fDictName";
            cbfclassstno.ValueMember = "fDictNo";
            cbfclassstno.SelectedIndex = 0;

            //执行查询代码
            OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }
        }

        //新增代码
        protected override void OnSys01()
        {
            uifpackno.Text = "";
            uifpackname.Text = "";
            qifpackno.Text = "";
            uifbarcode.Text = "";
            uifspec.Text = "";
            uifunit.Text = "";
            uifbuyprice.Text = "0";
            uifweight.Text = "1";
            uifvolume.Text = "0";
            uifclassstno.SelectedIndex = 0;
            uifstorageflag.SelectedIndex = 0;
            uifcolor.SelectedIndex = 0;

            sType = "A";
            doLoadData();
            uifpackno.ReadOnly = false;
            uifweight.ReadOnly = false;

            base.OnSys01();
        }

        //编辑+浏览载入数据
        private void doLoadData()
        {
            //控制界面输入
            Boolean input = false;
            if ((sType.Equals("A")) || (sType.Equals("B")))
            {
                input = false;
            }
            else
            {
                input = true;
            }
            foreach (Control ctrl in uiPanelData.Controls)
            {
                if (ctrl is UITextBox)
                {
                    UITextBox m = (UITextBox)ctrl;
                    m.ReadOnly = input;
                }

                if (ctrl is UIComboBox)
                {
                    UIComboBox m = (UIComboBox)ctrl;
                    m.ReadOnly = input;
                }
            }

            if (!sType.Equals("A"))
            {
                uifpackno.Text = uiDGView["fpackno", rowindex].Value.ToString();
                uifpackname.Text = uiDGView["fpackname", rowindex].Value.ToString();
                uifbarcode.Text = uiDGView["fbarcode", rowindex].Value.ToString();
                uifweight.Text = uiDGView["fweight", rowindex].Value.ToString();
                uifspec.Text = uiDGView["fspec", rowindex].Value.ToString();
                uifunit.Text = uiDGView["funit", rowindex].Value.ToString();
                uifvolume.Text = uiDGView["fvolume", rowindex].Value.ToString();
                uifbuyprice.Text = uiDGView["fbuyprice", rowindex].Value.ToString();

                int i = 0;
                string sfclassstno = uiDGView["fclassstno", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifclassstno.Items)
                {
                    string id = dr["fDictNo"].ToString();
                    if (id.Equals(sfclassstno))
                    {
                        uifclassstno.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }

                i = 0;
                string sfcolor = uiDGView["fcolor", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifcolor.Items)
                {
                    string id = dr["fDictNo"].ToString();
                    if (id.Equals(sfcolor))
                    {
                        uifcolor.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }

                i = 0;
                string sfstorageflag = uiDGView["fstorageflag", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifstorageflag.Items)
                {
                    string id = dr["fDictNo"].ToString();
                    if (id.Equals(sfstorageflag))
                    {
                        uifstorageflag.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }
            }

        }

        //编辑代码
        protected override void OnSys02()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "B";

            doLoadData();

            uifpackno.ReadOnly = true;
            uifweight.ReadOnly = false;

            base.OnSys02();
        }

        //删除代码
        protected override void OnSys03()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }

            if (this.ShowAskDialog("确定删除(" + uiDGView["fpackname", rowindex].Value.ToString() + ")?"))
            {
                string sBillNo = uiDGView["fpackno", rowindex].Value.ToString();
                string SQL = "exec proc_biz_dodelete 'i_ts_pack','','" + sBillNo + "',2,'" + sUser + "'";
                DataTable myTable = StaticClass.myDB.query(SQL);

                string sMsg = myTable.Rows[0][0].ToString();
                if (sMsg.Equals("OK"))
                {
                    this.ShowInfoDialog("删除完毕");
                    myCache.doCache();
                    OnSys04();
                }
                else
                {
                    this.ShowInfoDialog(sMsg);
                }
            }
        }

        //浏览代码
        protected override void OnSys10()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "V";

            doLoadData();

            base.OnSys10();

        }

        //数据为空检查
        private bool CheckData()
        {
            return CheckEmpty(uifpackno, "请输入容器编号")
                   && CheckEmpty(uifpackname, "请输入容器名称")
                   && CheckEmpty(uifweight, "请输入容器毛重")
                   && CheckEmpty(uifstorageflag, "请选择存储分类")
                   && CheckEmpty(uifcolor, "请选择容器颜色");
        }

        //保存代码
        protected override void OnSys08()
        {
            if (!CheckData())
            {
                return;
            }
            DataRowView drv1 = (DataRowView)uifcolor.SelectedItem;
            string sfcolor = drv1.Row["fDictNo"].ToString();

            DataRowView drv2 = (DataRowView)uifclassstno.SelectedItem;
            string sfclassstno = drv2.Row["fDictNo"].ToString();

            DataRowView drv3 = (DataRowView)uifstorageflag.SelectedItem;
            string sfstorageflag = drv3.Row["fDictNo"].ToString();

            if (uifbuyprice.Text == "")
            {
                uifbuyprice.Text = "0";
            }

            if (uifweight.Text == "")
            {
                uifweight.Text = "1";
            }

            if (uifvolume.Text == "")
            {
                uifvolume.Text = "1";
            }

            if (sType == "A")
            {
                try
                {
                    using (XTBasEntities context = new XTBasEntities())
                    {
                        i_ts_pack myData = context.i_ts_pack.Find(uifpackno.Text);
                        if (myData is null)
                        {
                            i_ts_pack myData2 = new i_ts_pack();
                            myData2.fpackno = uifpackno.Text;
                            myData2.fpackname = uifpackname.Text;
                            myData2.fbarcode = uifbarcode.Text;
                            myData2.fclassstno = uifclassstno.Text;
                            myData2.fspec = uifspec.Text;
                            myData2.funit = uifunit.Text;
                            myData2.fcolor = sfcolor;
                            myData2.fbuyprice = decimal.Parse(uifbuyprice.Text);
                            myData2.fweight = decimal.Parse(uifweight.Text);
                            myData2.fvolume = decimal.Parse(uifvolume.Text);
                            myData2.foperator = sUser;
                            myData2.fupddate = DateTime.Now;
                            myData2.fstorageflag = sfstorageflag;
                            myData2.fclassstno = sfclassstno;
                            context.i_ts_pack.Add(myData2);
                            context.SaveChanges();

                            UIMessageTip.ShowOk("添加成功");

                        }
                        else
                        {
                            //Console.WriteLine("添加成功");
                            UIMessageTip.ShowOk("编号已存在");
                            //Console.WriteLine("已存在");
                            return;
                        }

                    }
                }
                catch (DbEntityValidationException dbEx)
                {
                    Console.WriteLine("错误" + dbEx.Message);
                }
            }

            else if (sType == "B")
            {
                using (XTBasEntities context = new XTBasEntities())
                {
                    //查询
                    i_ts_pack myts_pack = context.i_ts_pack.Find(uifpackno.Text);

                    myts_pack.fpackno = uifpackno.Text;
                    myts_pack.fpackname = uifpackname.Text;
                    myts_pack.fbarcode = uifbarcode.Text;
                    myts_pack.fclassstno = uifclassstno.Text;
                    myts_pack.fspec = uifspec.Text;
                    myts_pack.funit = uifunit.Text;
                    myts_pack.fcolor = sfcolor;
                    myts_pack.fbuyprice = decimal.Parse(uifbuyprice.Text);
                    myts_pack.fweight = decimal.Parse(uifweight.Text);
                    myts_pack.fvolume = decimal.Parse(uifvolume.Text);
                    myts_pack.foperator = sUser;
                    myts_pack.fupddate = DateTime.Now;
                    myts_pack.fstorageflag = sfstorageflag;
                    myts_pack.fclassstno = sfclassstno;
                    int result = context.SaveChanges();
                    UIMessageTip.ShowOk("修改成功");
                }
            }

            //刷新cache
            sType = "V";
            base.OnSys08();

            OnSys04();
        }

        private void uiCBfstorageflag_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }

        private void cbfclassstno_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }
    }
}
